package com.zds.zhouyi.ui;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.tencent.mmkv.MMKV;
import com.zds.zhouyi.R;
import com.zds.zhouyi.model.Const;
import com.zds.zhouyi.model.History;
import com.zds.zhouyi.util.ResUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class HistoryDialog extends Dialog {

    ImageView ivClose;
    RecyclerView rvHistory;
    QuickHistoryAdapter quickHistoryAdapter;

    NormalDialog.Builder builder;

    Gson gson;

    public HistoryDialog(@NonNull Context context) {
        super(context);
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_history);
        ivClose = findViewById(R.id.iv_close);
        rvHistory = findViewById(R.id.rv_history);
        rvHistory.setLayoutManager(new LinearLayoutManager(getContext()));
        rvHistory.setItemAnimator(null);
        quickHistoryAdapter = new QuickHistoryAdapter();
        rvHistory.setAdapter(quickHistoryAdapter);
        quickHistoryAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ArrayList<History> list = (ArrayList<History>) adapter.getData();
                for (History history : list) {
                    history.setShow(list.indexOf(history) == position);
                }
                quickHistoryAdapter.notifyDataSetChanged();
            }
        });
        quickHistoryAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (builder == null) {
                    builder = new NormalDialog.Builder(getContext());
                }
                builder.setPositiveButton(new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (gson == null) {
                            gson = new Gson();
                        }
                        adapter.getData().remove(position);
                        MMKV.defaultMMKV().encode(Const.HISTORY_KEY, gson.toJson(adapter.getData()));
                        quickHistoryAdapter.notifyItemRemoved(position);
                    }
                });
                builder.setNegativeButton(new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                });
                builder.create().show();
                return false;
            }
        });
        quickHistoryAdapter.setEmptyView(R.layout.empty_item);
        View footerView = getLayoutInflater().inflate(R.layout.footer_item_clear_all, rvHistory, false);
        quickHistoryAdapter.addFooterView(footerView);
        footerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (builder == null) {
                    builder = new NormalDialog.Builder(getContext());
                }
                builder.setContent(ResUtil.getString(R.string.clear_all_tip));
                builder.setPositiveButton(new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MMKV.defaultMMKV().remove(Const.HISTORY_KEY);
                        quickHistoryAdapter.setList(new ArrayList<>());
                    }
                });
                builder.setNegativeButton(new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                });
                builder.create().show();
            }
        });
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    private List<History> getData() {
        MMKV mmkv = MMKV.defaultMMKV();
        Gson gson = new Gson();
        // 定义 Type 对象来保留泛型信息
        Type historyListType = new TypeToken<List<History>>() {
        }.getType();

        // 将 JSON 字符串转换为 History 列表
        List<History> historyList = gson.fromJson(mmkv.decodeString(Const.HISTORY_KEY), historyListType);

        if (historyList != null && !historyList.isEmpty()) {
            return historyList;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 显示 loading 弹窗
     */
    public void showDialog() {
        quickHistoryAdapter.setList(getData());
        if (!isShowing()) {
            show();
        }
    }

    /**
     * 隐藏 loading 弹窗
     */
    public void hide() {
        if (isShowing()) {
            dismiss();
        }
    }
}
